package com.rizzo.android.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import com.actionbarsherlock.app.ActionBar;
import com.rizzo.android.R;
import com.rizzo.android.ui.adapter.MainPagerAdapter;
import com.rizzo.android.ui.fragment.InfoFragment;
import com.rizzo.android.ui.fragment.NavigationListFragment;
import com.rizzo.android.ui.slidingmenu.SlidingMenu;

/**
 * This is the first Activity that is started. It contains all the logic for the Sliding-menu
 * and pager stuff.
 */
public class MainPagerActivity extends BaseActivity implements ActionBar.OnNavigationListener {

    private ViewPager viewPager;

    public MainPagerActivity() {
        super(R.string.empty);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        String[] planets = {"Mercury", "Venus", "Earth",
                "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
        ArrayAdapter spinnerAdapter = new ArrayAdapter(actionBar.getThemedContext(), android.R.layout.simple_spinner_dropdown_item, planets);
        actionBar.setListNavigationCallbacks(spinnerAdapter, this);
        Log.d("MainPagerActivity", "onCreate");
        this.viewPager = new ViewPager(this);
        viewPager.setId("MainPager".hashCode());
        final MainPagerAdapter adapter = new MainPagerAdapter(this);
        adapter.addFragment(0, InfoFragment.class);
        adapter.addFragment(1, InfoFragment.class);
        adapter.addFragment(2, InfoFragment.class);
        adapter.addFragment(3, InfoFragment.class);
        adapter.addFragment(4, InfoFragment.class);
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(5);
        final RetardationPageTransformer pageTransformer = new RetardationPageTransformer(this);
        viewPager.setPageTransformer(true, pageTransformer);


        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        final float screenWidth = display.getWidth() / 2;


        viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int position) {

            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                ImageView shadowOne = (ImageView) viewPager.findViewWithTag("shadow-" + (position + 1));
                if (shadowOne != null) {
                    if (positionOffset < .98f) {
                        shadowOne.setAlpha(1f);
                    } else {
                        shadowOne.setAlpha(-50f * positionOffset + 50f);
                    }
                    float off = (positionOffset) * (screenWidth);
                    float translationX = screenWidth - off;
                    shadowOne.setTranslationX(translationX);
                }
            }

            @Override
            public void onPageSelected(int position) {
                getNavigationFragment().setCurrentPosition(position);
                ActionBar supportActionBar = getSupportActionBar();
                NavigationListFragment.NavigationItem navigationItem = getNavigationFragment().getCurrentNavigationItem();
                supportActionBar.setLogo(getResources().getDrawable(navigationItem.icon));

                ImageView shadow = (ImageView) viewPager.findViewWithTag("shadow-" + position);
                if (shadow != null) {
                    shadow.setAlpha(0f);
                }

                switch (position) {
                    case 0:
                        getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                        break;
                    default:
                        getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
                        break;
                }
            }

        });

        setContentView(viewPager);

        getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        setSlidingActionBarEnabled(false);
    }

    public void changePage(View view, final int position) {
        Log.d("MainPagerActivity", "changePage");
        if (position >= 0) {
            final ViewPager pager = getViewPager();
            final SlidingMenu slidingMenu = getSlidingMenu();
            view.post(new Runnable() {
                public void run() {
                    slidingMenu.toggle();
                    pager.setCurrentItem(position, false);
                }
            });
        }
    }

    public ViewPager getViewPager() {
        return viewPager;
    }

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        return false;
    }
}
